package com.zmn.oms.task.job.third.jddj;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.third.jddj.JddjConsts;
import com.zmn.oms.third.jddj.service.JddjService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 类描述：京东到家渠道创建订单任务，5秒后执行
 *
 * @author xiewenbing
 * @date 2021/01/20 1545
 */
@LTS
@Slf4j
public class JddjCreateOrderJob {

    @Autowired
    private JddjService jddjService;

    /**
     * 创建订单任务
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_JDDJ_ADD_CREATE_JOB)
    public Result runWorkJob(Job job) {

        if (!JddjConsts.POWER_ON) {
            log.info("【{}】<创建订单> 该功能没有开启。", JddjConsts.CHANNEL_NAME);
            return new Result(Action.EXECUTE_FAILED, "不需要执行!");
        }

        String channelOrderId = job.getParam("outerId");
        try {
            // 创建订单
            jddjService.createOrderByChannelOrderId(channelOrderId);
        } catch (OmsBaseException e) {
            log.info("【{}】<创建订单>任务异常 --【{}】", JddjConsts.CHANNEL_NAME,e.getMessage());
            return new Result(Action.EXECUTE_FAILED, e.getMessage());
        }

        log.info("#oms#jddj_create_order#创建订单任务");
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
